var express = require('express');
var app = express();
app.use(express.static('static'));
//post请求的中间件
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

//生成返回数据
var arr = [
    { email: '178884773@qq.com', name: '张伟', phone: '15229072151', pass: '1234', ingroup: '编辑组', isadmin: true,  status: true },
    { email: '178884773@qq.com', name: '李煜', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true,  status: true },
    { email: '178884773@qq.com', name: '李路', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true,  status: false },
    { email: '178884773@qq.com', name: '张力', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true,  status: true },
    { email: '178884773@qq.com', name: '杨林', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true,  status: true },
    { email: '178884773@qq.com', name: '杨戏', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true,  status: false },
    { email: '178884773@qq.com', name: '张晓', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true,  status: true },
    { email: '178884773@qq.com', name: '王伟', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true,  status: true },
    { email: '178884773@qq.com', name: '杨凯', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true,  status: false },
    { email: '178884773@qq.com', name: '胡立', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true,  status: true },
    { email: '178884773@qq.com', name: '王琳', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true,  status: true },
    { email: '178884773@qq.com', name: '王往', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true,  status: false },
    { email: '178884773@qq.com', name: '程将', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true,  status: true },
    { email: '178884773@qq.com', name: '刘欢', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true,  status: true },
    { email: '178884773@qq.com', name: '刘莉', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true,  status: false },
    { email: '178884773@qq.com', name: '王浩', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true,  status: true },
    { email: '178884773@qq.com', name: '李曼', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true,  status: true },
    { email: '178884773@qq.com', name: '杨倩', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true,  status: false }
]

//分页请求
app.get('/user/list', (req, res) => {
    var { page, count } = req.query;
    var newarr = arr.slice((page - 1) * count, page * count);
    res.send({ code: 200, msg: '请求成功!', list: newarr });
})

//搜索
app.get('/user/search', (req, res) => {
    var { searchkey } = req.query;
    var newarr = arr.filter((item) => {
        return new RegExp(searchkey).test(item.name);
    })
    res.send({ code: 200, msg: '查询成功!', list: newarr });
})

//登录
app.post('/user/login', (req, res) => {
    var { phone, pass } = req.body;
    var i = arr.findIndex((item, index) => {
        return item.phone == phone && item.pass == pass;
    })
    if (i != -1) {
        res.send({ code: 200, msg: '登陆成功!' });
    } else {
        res.send({ code: 400, msg: '登陆失败!' });
    }
})

//设置监听
app.listen(2022, () => {
    console.log('服务端运行在: http://localhost:2022');
})
